package cn.ming.wfchat.kit.conversation.message.viewholder;

import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.ming.wfchat.kit.R2;
import cn.ming.wfchat.kit.annotation.MessageContentType;
import cn.ming.wfchat.kit.conversation.ConversationFragment;
import cn.ming.wfchat.kit.conversation.message.model.UiMessage;
import cn.wildfirechat.message.AddParticipantsMessageContent;
import cn.wildfirechat.message.Message;
import cn.wildfirechat.message.WithdrawAllContent;
import cn.wildfirechat.message.notification.AddGroupMemberNotificationContent;
import cn.wildfirechat.message.notification.ChangeGroupNameNotificationContent;
import cn.wildfirechat.message.notification.ChangeGroupPortraitNotificationContent;
import cn.wildfirechat.message.notification.CreateGroupNotificationContent;
import cn.wildfirechat.message.notification.DismissGroupNotificationContent;
import cn.wildfirechat.message.notification.FriendAddedMessageContent;
import cn.wildfirechat.message.notification.FriendGreetingMessageContent;
import cn.wildfirechat.message.notification.GroupJoinTypeNotificationContent;
import cn.wildfirechat.message.notification.GroupMuteMemberNotificationContent;
import cn.wildfirechat.message.notification.GroupMuteNotificationContent;
import cn.wildfirechat.message.notification.GroupPrivateChatNotificationContent;
import cn.wildfirechat.message.notification.GroupSetManagerNotificationContent;
import cn.wildfirechat.message.notification.KickoffGroupMemberNotificationContent;
import cn.wildfirechat.message.notification.ModifyGroupAliasNotificationContent;
import cn.wildfirechat.message.notification.NotificationMessageContent;
import cn.wildfirechat.message.notification.QuitGroupNotificationContent;
import cn.wildfirechat.message.notification.RecallMessageContent;
import cn.wildfirechat.message.notification.RedPageGroupMemberNotificationContent;
import cn.wildfirechat.message.notification.TipNotificationContent;
import cn.wildfirechat.message.notification.TransferGroupOwnerNotificationContent;
import cn.wildfirechat.remote.ChatManager;


@MessageContentType(value = {
        AddGroupMemberNotificationContent.class,
        ChangeGroupNameNotificationContent.class,
        ChangeGroupPortraitNotificationContent.class,
        CreateGroupNotificationContent.class,
        DismissGroupNotificationContent.class,
        DismissGroupNotificationContent.class,
        KickoffGroupMemberNotificationContent.class,
        ModifyGroupAliasNotificationContent.class,
        QuitGroupNotificationContent.class,
        TransferGroupOwnerNotificationContent.class,
        TipNotificationContent.class,
        FriendAddedMessageContent.class,
        FriendGreetingMessageContent.class,
        RecallMessageContent.class,
        GroupMuteNotificationContent.class,
        GroupPrivateChatNotificationContent.class,
        GroupJoinTypeNotificationContent.class,
        GroupSetManagerNotificationContent.class,
        GroupMuteMemberNotificationContent.class,
        AddParticipantsMessageContent.class,
        RedPageGroupMemberNotificationContent.class,
        WithdrawAllContent.class
        // TODO add more

})
/**
 * 小灰条消息, 居中显示，且不显示发送者，用于简单通知，如果需要扩展成复杂通知，可以参考 {@link ExampleRichNotificationMessageContentViewHolder}
 *
 */
public class SimpleNotificationMessageContentViewHolder extends NotificationMessageContentViewHolder {

    @BindView(R2.id.notificationTextView)
    TextView notificationTextView;

    public SimpleNotificationMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    public void onBind(UiMessage message, int position) {
        super.onBind(message, position);
        onBind(message);
    }

    @Override
    public boolean contextMenuItemFilter(UiMessage uiMessage, String tag) {
        return true;
    }

    protected void onBind(UiMessage message) {
        String notification;
        try {
            notification = ((NotificationMessageContent) message.message.content).formatNotification(message.message);
        } catch (Exception e) {
            e.printStackTrace();
            notification = "message is invalid";
        }
        notificationTextView.setText(notification);
        if (message.message.content instanceof WithdrawAllContent) {
            WithdrawAllContent withdrawAllContent = (WithdrawAllContent) message.message.content;
            List<Long> list = JSON.parseArray(withdrawAllContent.getMids(), Long.class);
            for (Long id : list) {
                Message deleMessage = ChatManager.Instance().getMessageByUid(id);
                if (deleMessage != null) {
                    messageViewModel.deleteMessage(deleMessage);
                }
            }
        }
//        else if (message.message.content instanceof KickoffGroupMemberNotificationContent) {
//            KickoffGroupMemberNotificationContent kickoffGroupMemberNotificationContent = (KickoffGroupMemberNotificationContent) message.message.content;
//            if (kickoffGroupMemberNotificationContent.kickedMembers != null) {
//                for (String member : kickoffGroupMemberNotificationContent.kickedMembers) {
//                    if (TextUtils.equals(member, ChatManager.Instance().getUserId())) {
//                        messageViewModel.removeConversation(new Conversation(Conversation.ConversationType.Group, kickoffGroupMemberNotificationContent.groupId, 0), true);
//
//                    }
//                }
//            }
//
//        }
    }

}
